<?php

class CustomcaseController extends Controller {

    public function actionIndex() {
	if (isset($_POST['submit'])) {
	    if ($_POST['img_canvas']) {
		//get code of image, remove information like: "data:image/png;base64,"
		$filteredData = substr($_POST['img_canvas'], strpos($_POST['img_canvas'], ",") + 1);
		$imgCanvas = base64_decode($filteredData);
		file_put_contents(UPLOAD_PATH . DIRECTORY_SEPARATOR . 'customcase' . DIRECTORY_SEPARATOR . uniqid() . '.png', $imgCanvas); //create an image
	    }
	}
	$this->render('index');
    }

    public function actionOrder($id) {
	$imageUploaded = '';
	$model = CustomPhone::model()->findbyPk($id);
	if ($model) {
	    if (isset($_POST['submit'])) {
		if (is_numeric($_POST['total_order'])) {
		    $imgName = '';
		    //store designed image
		    if ($_POST['img_canvas']) {
			//get code of image, remove information like: "data:image/png;base64,"
			$filteredData = substr($_POST['img_canvas'], strpos($_POST['img_canvas'], ",") + 1);
			$imgCanvas = base64_decode($filteredData);
			$imgName = uniqid() . '.png';
			file_put_contents(EXPORT_DESIGN_PHONE_PATH . $imgName, $imgCanvas); //create an image
		    }

		    $totalPurchase = floatval($model->price) * intval($_POST['total_order']);
		    if ($model->sale) {
			$sale = $totalPurchase * floatval(floatval($model->sale) / 100);
			$totalPurchase = $totalPurchase - $sale;
		    }
		    $position = array('top' => $_POST['top-img'], 'left' => $_POST['left-img']);

		    $rate = floatval(intval($_POST['real-width']) / intval($_POST['custom-width']));

		    //save the ordering information
		    $order = array(
			'model_id' => OrderDetail::MODEL_CUSTOM_PHONE,
			'name' => $model->name,
			'total_order' => $_POST['total_order'],
			'total_purchase' => $totalPurchase,
			'custom_phone_id' => $id,
			'custom_width' => $_POST['custom-width'],
			'real_width' => $_POST['real-width'],
			'position' => serialize($position),
			'image_designed' => $imgName,
			'image_upload' => $_POST['image-uploaded'],
			'degree' => $_POST['degree-img'],
			'rate' => $rate
		    );

		    if (isset(Yii::app()->session['order'])) {
			$aOrder = Yii::app()->session['order'];
			array_push($aOrder, $order);
			Yii::app()->session['order'] = $aOrder;
		    } else {
			Yii::app()->session['order'] = array(0 => $order);
		    }


		    if (isset(Yii::app()->session['image_uploaded'])) {
			unset(Yii::app()->session['image_uploaded']);
		    }
		    //add to cart	    
		    $this->redirect(url('cart'));
		} else {
		    Yii::app()->user->setFlash('error', 'Quantity must be a number');
		}
	    }

	    //if user uploaded before, but they didn't add to their cart
	    if (isset(Yii::app()->session['image_uploaded'])) {
		$imageUploaded = IMG_UPLOAD_URL . Yii::app()->session['image_uploaded'];
	    }
	    $this->render('order', array('model' => $model, 'imageUploaded' => $imageUploaded));
	} else {
	    throw new CHttpException(400, 'The page is not found');
	}
    }

    public function actionEdit($id) {

	//search in session
	if (isset(Yii::app()->session['order'])) {
	    $aOrder = Yii::app()->session['order'];

	    if (array_key_exists($id, $aOrder)) {

		if ($aOrder[$id]['model_id'] == OrderDetail::MODEL_CUSTOM_PHONE) {
		    $model = CustomPhone::model()->findbyPk($aOrder[$id]['custom_phone_id']);
		    if (isset($_POST['submit'])) {
			if (is_numeric($_POST['total_order'])) {
			    $imgName = '';
			    //store designed image
			    if ($_POST['img_canvas']) {
				//get code of image, remove information like: "data:image/png;base64,"
				$filteredData = substr($_POST['img_canvas'], strpos($_POST['img_canvas'], ",") + 1);
				$imgCanvas = base64_decode($filteredData);
				$imgName = uniqid() . '.png';
				file_put_contents(EXPORT_DESIGN_PHONE_PATH . $imgName, $imgCanvas); //create an image
			    }

			    $totalPurchase = floatval($model->price) * intval($_POST['total_order']);

			    if ($model->sale) {
				$sale = $totalPurchase * floatval(floatval($model->sale) / 100);
				$totalPurchase = $totalPurchase - $sale;
			    }
			    $position = array('top' => $_POST['top-img'], 'left' => $_POST['left-img']);

			    $rate = floatval(intval($_POST['real-width']) / intval($_POST['custom-width']));

			    //save the ordering information
			    $aOrder[$id]['total_order'] = $_POST['total_order'];
			    $aOrder[$id]['total_purchase'] = $totalPurchase;
			    $aOrder[$id]['custom_width'] = $_POST['custom-width'];
			    $aOrder[$id]['real_width'] = $_POST['real-width'];
			    $aOrder[$id]['position'] = serialize($position);
			    $aOrder[$id]['image_designed'] = $imgName;
			    $aOrder[$id]['image_upload'] = $_POST['image-uploaded'];
			    $aOrder[$id]['degree'] = $_POST['degree-img'];
			    $aOrder[$id]['rate'] = $rate;

			    Yii::app()->session['order'] = $aOrder;

			    if (isset(Yii::app()->session['image_uploaded'])) {
				unset(Yii::app()->session['image_uploaded']);
			    }
			    //add to cart	    
			    $this->redirect(url('cart'));
			} else {
			    Yii::app()->user->setFlash('error', 'Quantity must be a number');
			}
		    }

		    $imageUploaded = IMG_UPLOAD_URL . $aOrder[$id]['image_upload'];
		    $postion = unserialize($aOrder[$id]['position']);
		    $this->render('edit', array(
			'order' => $aOrder[$id],
			'position' => $postion,
			'model' => $model,
			'imageUploaded' => $imageUploaded
		    ));
		} else {
		    $err = 1;
		}
	    } else {
		$err = 1;
	    }
	} else {
	    $err = 1;
	}
	if (isset($err)) {
	    throw new CHttpException(400, 'The page was not found!');
	}
    }

    /**
     * save image that was uploaded recently
     */
    public function actionMark_uploaded() {
	if (isset($_POST['image'])) {
	    Yii::app()->session['image_uploaded'] = $_POST['image'];
	}
    }

}

